﻿&"D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars32.bat"

$SolutionDir = "D:\Work\Bridge6"
# 设置编码为UTF-8
$OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# 执行Git操作
git checkout master
git reset --hard

# 切换到libs目录，执行依赖库构建脚本
Set-Location -Path "$SolutionDir\libs"
Write-Host ""1. 构建hiredis库...""
.\mk_1_hiredis.ps1

Set-Location -Path "$SolutionDir\libs"
Write-Host ""2. 构建redis++库...""
.\mk_2_redis++.ps1

Set-Location -Path "$SolutionDir\libs"
Write-Host ""3. 构建mysql-connector库...""
.\mk_3_mysql-connector-c++.ps1

Set-Location -Path "$SolutionDir\libs"
Write-Host ""4. 构建boost库...""
.\mk_4_boost.ps1

Set-Location -Path "$SolutionDir\libs"
Write-Host ""5. 构建googletest库...""
.\mk_5_googletest.ps1

Set-Location -Path "$SolutionDir\libs"
Write-Host ""6. 构建vld库...""
.\mk_6_vld.ps1

# 构建analysis项目
Write-Host ""7. 构建analysis/Release库...""
Set-Location -Path "$SolutionDir\analysis"
New-Item -Path "build\win32_release" -ItemType Directory -Force | Out-Null
Set-Location -Path "build\win32_release"
cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=Win32
ninja -v -j8
ninja install

Write-Host ""8. 构建analysis/Debug库...""
Set-Location -Path "$SolutionDir\analysis"
New-Item -Path "build\win32_debug" -ItemType Directory -Force | Out-Null
Set-Location -Path "build\win32_debug"
cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=Win32
ninja -v -j8
ninja install

# 构建BridgeAnalysis项目
Write-Host ""9. 构建BridgeAnalysis/Release库...""
Set-Location -Path "$SolutionDir\BridgeAnalysis"
New-Item -Path "build\win32_release" -ItemType Directory -Force | Out-Null
Set-Location -Path "build\win32_release"
cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=Win32
ninja -v -j8
ninja install

Write-Host ""10. 构建BridgeAnalysis/Debug库...""
Set-Location -Path "$SolutionDir\BridgeAnalysis"
New-Item -Path "build\win32_debug" -ItemType Directory -Force | Out-Null
Set-Location -Path "build\win32_debug"
cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=Win32
ninja -v -j8
ninja install

# 构建ChemDLL项目（编号保持与原脚本一致，此处为"9"）
Write-Host ""11. 构建ChemDLL/Release库...""
Set-Location -Path "$SolutionDir\ChemDLL"
New-Item -Path "build\win32_release" -ItemType Directory -Force | Out-Null
Set-Location -Path "build\win32_release"
cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=Win32
ninja -v -j8
ninja install

Write-Host ""12. 构建ChemDLL/Debug库...""
Set-Location -Path "$SolutionDir\ChemDLL"
New-Item -Path "build\win32_debug" -ItemType Directory -Force | Out-Null
Set-Location -Path "build\win32_debug"
cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=Win32
ninja -v -j8
ninja install

# 构建BridgeTest项目（编号保持与原脚本一致，此处为"11"和"12"）
Write-Host ""13. 构建BridgeTest/Release执行程序...""
Set-Location -Path "$SolutionDir\BridgeTest"
New-Item -Path "build\win32_release" -ItemType Directory -Force | Out-Null
Set-Location -Path "build\win32_release"
cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=Win32
ninja -v -j8
ninja install

Write-Host ""14. 构建BridgeTest/Debug执行程序...""
Set-Location -Path "$SolutionDir\BridgeTest"
New-Item -Path "build\win32_debug" -ItemType Directory -Force | Out-Null
Set-Location -Path "build\win32_debug"
cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=Win32
ninja -v -j8
ninja install

# 构建LogService项目（编号保持与原脚本一致，此处为"11"和"12"）
Write-Host ""13. 构建LogService/Release执行程序...""
Set-Location -Path "$SolutionDir\LogService"
New-Item -Path "build\win32_release" -ItemType Directory -Force | Out-Null
Set-Location -Path "build\win32_release"
cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=Win32
ninja -v -j8
ninja install

Write-Host ""14. 构建LogService/Debug执行程序...""
Set-Location -Path "$SolutionDir\LogService"
New-Item -Path "build\win32_debug" -ItemType Directory -Force | Out-Null
Set-Location -Path "build\win32_debug"
cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=Win32
ninja -v -j8
ninja install

# 运行测试程序
Write-Host ""17. 测试BridgeTest/Release执行程序...""
Set-Location -Path "$SolutionDir\Win32\Release"
.\BridgeTest.exe --gtest_filter=AnalysisTest2*
.\BridgeTest.exe --gtest_filter=ChemDLLTest1*

Write-Host ""18. 测试BridgeTest/Debug执行程序...""
Set-Location -Path "$SolutionDir\Win32\Debug"
.\BridgeTest.exe --gtest_filter=AnalysisTest2*
.\BridgeTest.exe --gtest_filter=ChemDLLTest1*

# 回到初始目录
Set-Location -Path "$SolutionDir"